<?php
// digiboard by digitalboom.org
// http://www.digitalboom.org
//
// Information Technology program
// Sirindhorn International Institute of Technology
// Thammasat University, Rangsit Campus
// http://www.siit.tu.ac.th
//
// authors: hoho, bact', sea
// first created on: 2002 Mar 10
// last modified on: 2008 May 14
// last modified by: sea

function SendEmail_NewUser($Name, $Lastname, $Email, $Username, $Password,
 $WEBBOARD_NAME, $ADMIN_EMAIL, $URLPATH, $UseMailFunction) {
 global $DGB;

 if($UseMailFunction == false) {
  include("zendmail.php");
 }
 
 $Subject  = "Welcome to ".$WEBBOARD_NAME;
 $message .= "Thank you for joining $WEBBOARD_NAME\n";
 $message .= "Your login name = $Username\n";
 $message .= "Your password = $Password\n\n";
 $message .= "$URLPATH";

 if ($UseMailFunction == false) {
  zendmail('$ADMIN_EMAIL','$Email','$Subject','$message');
 } else {
  mail($Email,$Subject,$message,"From: $WEBBOARD_NAME <$DGB[ADMIN_EMAIL]>\r\n"."Reply-To: $DGB[ADMIN_EMAIL]\r\n"."X-Mailer: PHP/" . phpversion());
 }
}

function CheckUser($Username, $Password) {
 global $db_link, $DGB;
 
 $db_link = dgb_db_connect();
 stripslashes($Username);

 $NowTimeStamp  = time();
 $NowTimeStamp_Offsetted  = $NowTimeStamp-($DGB["TIME_OFFSET"]*3600);
 $LastOnlineDateTime = date("Y-m-d H:i:s", $NowTimeStamp_Offsetted);

 //Check Username
 $query = "SELECT * FROM ".$DGB["DB_USERS_TABLE"]." WHERE Username='$Username' AND Password='".md5($Password)."'";
 $result = mysql_query($query, $db_link);
 $num = mysql_num_rows($result);
 if ($num>0) {
  $AlreadyLogin = 1;

  //Update LoginTime
  $query1 = "UPDATE ".$DGB["DB_USERS_TABLE"]." SET LastOnlineTime = '$LastOnlineDateTime' WHERE Username='$Username' AND Password='".md5($Password)."'";
  $result1 = mysql_query($query1,$db_link);
 } else {
  $AlreadyLogin = 0;
 }
 
 return $AlreadyLogin;
}

function SendUserPass($Username) {
 global $db_link, $DGB;
 
 $db_link = dgb_db_connect();
 stripslashes($Username);

 //Check Valid Username
 $query = "SELECT Password,Email FROM ".$DGB["DB_USERS_TABLE"]." WHERE Username='$Username'";
 $result = mysql_query($query, $db_link);
 $num = mysql_num_rows($result);
 if ($num>0) {
	$ValidUser = 1;
	$data  = mysql_fetch_row($result);
	$Password = $data[0];
	$Email = $data[1];

 //Send Mail
 if($DGB["SendMailSystem"] == false) {
  include("zendmail.php");
 }
 
 $Subject  = "You are requesting to reset password of SIIT.NET Webboard";
 $message .= "If you did not submit this request, just ignore it.\n\n";
 $message .= "To reset the password, copy follow link into your web browser address or just click it :\n
 http://www.siit.net/webboard/resetpassword.php?username=$Username&resetkey=$Password\n";
 $message .= "Thank you very much, Admin\n\n";
 $message .= "".$DGB["HTTP_PATH"]."";

 if ($DGB["SendMailSystem"] == false) {
  zendmail('$DGB["ADMIN_EMAIL"]','$Email','$Subject','$message');
 } else {

  $MailFrom = $DGB["WEBBOARD_NAME"]." <".$DGB["ADMIN_EMAIL"].">";
  mail($Email,$Subject,$message,"From: $MailFrom\r\n"."Reply-To: $DGB[ADMIN_EMAIL]\r\n"."X-Mailer: PHP/" . phpversion());
 }
  
 } else {
  $ValidUser = 0;
 }

 return $ValidUser;
}


function SelectUserDetail($Username, $Password) {
 global $DGB,$db_link;
 
 $db_link = dgb_db_connect();
 stripslashes($Username);
 $NowTimeStamp  = time();
 $NowTimeStamp_Offsetted  = $NowTimeStamp-($DGB["TIME_OFFSET"]*3600);
 $LastOnlineDateTime = date("Y-m-d H:i:s", $NowTimeStamp_Offsetted);

 //Check Username
 $query="SELECT * FROM ".$DGB["DB_USERS_TABLE"]." WHERE Username='$Username' AND Password='$Password'";
 $result = mysql_query($query, $db_link);
 $num= mysql_num_rows($result);

 $Email = "";
 $ICQ   = "";
 $MSN   = "";
 if ($num > 0) {
  $data  = mysql_fetch_row($result);
  $Email = $data[3];
  $ICQ   = $data[4];
  $MSN   = $data[5];

  //Update LoginTime
  $query1 = "UPDATE ".$DGB["DB_USERS_TABLE"]." SET LastOnlineTime = '$LastOnlineDateTime' WHERE  Username='$Username' AND Password='$Password'";
  $result1 = mysql_query($query1,$db_link);
 }
 
 $UserDetail = "$Email|$ICQ|$MSN";

 return $UserDetail;
}

function UserOnline() {
 global $DGB,$db_link;

 //UNIX_TIMESTAMP(); mysql;

 $db_link = dgb_db_connect();
 $query = "SELECT Username , ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(LastOnlineTime)) AS TimeDiff,Email FROM ".$DGB["DB_USERS_TABLE"]." WHERE ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(LastOnlineTime)) <= '500' ORDER BY TimeDiff";
 $result = mysql_query($query,$db_link);
 $usernum = mysql_num_rows($result);
 $ListUsersOnline = "";
 
 if ($usernum > 0) {
  for ($i=0; $i<$usernum; $i++) {
   $data = mysql_fetch_row($result);
   $Username[$i] = $data[0];
   $TimeDiff[$i] = $data[1];
   $Email = $data[2];
   if ($i < ($usernum - 1)) {
    $ListUsersOnline .= "<a class='linkBtn2' href=\"javascript:nw('profile','profile.php?ProfileName=$Username[$i]',350,500,0)\"  title='$Email'>$Username[$i]</a>";
	  $ListUsersOnline .= ", ";
   } else {
    $ListUsersOnline .= "<a class='linkBtn2' href=\"javascript:nw('profile','profile.php?ProfileName=$Username[$i]',350,500,0)\"  title='$Email'>$Username[$i]</a>";
   }
  }
 }

 if ($usernum <= 0) {
  return "";
 } elseif ($usernum == 1) {
  return "1 <b>member online:</b> $ListUsersOnline";
 } else {
  return "$usernum <b>members online:</b> $ListUsersOnline";
 }
}

function AllBoardsStats($BoardTable) {
 global $db_link;
 
 $db_link = dgb_db_connect();
 $query = "SELECT COUNT(*) FROM ".$BoardTable."";
 $result = mysql_query($query,$db_link);
 $data = mysql_fetch_row($result);
 return "$data[0]";
}

function GuestOnline($SID) {
 global $db_link, $DGB;

 $db_link = dgb_db_connect();
 $NowTimeStamp  = time();
 $NowTimeStamp_Offsetted  = $NowTimeStamp-($DGB["TIME_OFFSET"]*3600);

 $OnlineDateTime = date("Y-m-d H:i:s", $NowTimeStamp_Offsetted);

 $query = "SELECT COUNT(*) FROM ".$DGB['DB_GUEST_TABLE']." WHERE SID = '$SID'";
 $result = mysql_query($query,$db_link);
 $data = mysql_fetch_row($result);
 if ($data[0]==0) {
  //New guest
  $queryInsert = "INSERT INTO ".$DGB['DB_GUEST_TABLE']." VALUES ('$SID','$OnlineDateTime')";
  $resultInsert = mysql_query($queryInsert,$db_link) or die("Can't Insert");
 } else {
  //Same guest
  $queryUpdate = "UPDATE ".$DGB['DB_GUEST_TABLE']." SET OnlineTime ='$OnlineDateTime' WHERE SID = '$SID'";
  $resultUpdate = mysql_query($queryUpdate,$db_link) or die("Can't Update");
 }

 // count all guests (include members)
 $query1 = "SELECT COUNT(*) FROM ".$DGB['DB_GUEST_TABLE']." WHERE  ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(OnlineTime)) <='500' ";
 $result1 = mysql_query($query1,$db_link);
 $data1 = mysql_fetch_row($result1);
 $AllUsers =  $data1[0];

 // delete old guest session
 $query2 = "DELETE FROM ".$DGB['DB_GUEST_TABLE']." WHERE  ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(OnlineTime)) > '500' ";
 $result2 = mysql_query($query2,$db_link);
 
 return $AllUsers;
}

function OnlyGuestNum($AllGuestNum) {
 global $db_link, $DGB;

 //UNIX_TIMESTAMP(); mysql;

 $db_link = dgb_db_connect();
 $query = "SELECT Username , ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(LastOnlineTime)) AS TimeDiff FROM ".$DGB["DB_USERS_TABLE"]." WHERE ((UNIX_TIMESTAMP() - ($DGB[TIME_OFFSET]*3600)) - UNIX_TIMESTAMP(LastOnlineTime)) <= '500' ORDER BY TimeDiff";
 $result = mysql_query($query,$db_link);
 $membersnum = mysql_num_rows($result);
 $OnlyGuest = $AllGuestNum - $membersnum;

 if ($OnlyGuest==0||$OnlyGuest<0) {
  $GuestList = "";
 }
 elseif ($OnlyGuest==1) {
 $GuestList = "1 <b>guest</b>";
 } else {
  $GuestList = "$OnlyGuest <b>guests</b>";
 }
 
 return $GuestList;
}

function HappyBirthday(){
 global $DGB;

 $NowTimeStamp  = time();
 $NowTimeStamp_Offsetted  = $NowTimeStamp-($DGB["TIME_OFFSET"]*3600);
 $NowDate = date("d", $NowTimeStamp_Offsetted);
 $NowMonth = date("m", $NowTimeStamp_Offsetted);
 $NowYear = date("Y", $NowTimeStamp_Offsetted);

 $ListName = "";

 $db_link = dgb_db_connect();
 $query = "SELECT Username,Birthdate FROM ".$DGB["DB_USERS_TABLE"]."";
 $result = mysql_query($query,$db_link);
 $num = mysql_num_rows($result);

 for ($i=0;$i<$num;$i++) {
  $data = mysql_fetch_row($result);
  list($userday,$usermonth,$useryear)= explode("/",$data[1]);

  if(($userday=="$NowDate")&&($usermonth=="$NowMonth")) {
   $HowOld = ($NowYear+0) - ($useryear+0);
   $ListName .= " <img src=\"smilies/messenger/cake.gif\" height=\"19\" width=\"19\" align=\"absmiddle\"> <a class='linkBtn2' href=\"javascript:nw('profile','profile.php?ProfileName=$data[0]',350,440,0)\" title='$HowOld years'><b>$data[0]</b> ($HowOld)</a> ";
  }
 }
 if ($ListName!="") {
  return "Happy Birthday : $ListName";
 } else{
  return "";
 }
}

function CheckRanking($TotalPoints) {
 global $DGB;

//50 points, 100 points , 200 points
if(($TotalPoints > 0) && ($TotalPoints <= 200)){$UserRanking = "<img src=\"smilies/messenger/star.gif\" height=19 width=19>";}
if(($TotalPoints > 200) && ($TotalPoints <= 400)){$UserRanking = "<img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19>";}
if(($TotalPoints > 400) && ($TotalPoints <= 600)){$UserRanking = "<img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19>";}
if(($TotalPoints > 600) && ($TotalPoints <= 800)){$UserRanking = "<img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19>";}
if(($TotalPoints > 800) && ($TotalPoints <= 1000)){$UserRanking = "<img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19><img src=\"smilies/messenger/star.gif\" height=19 width=19>";}
if(($TotalPoints > 1000) && ($TotalPoints <= 10000)){$UserRanking = "<img src=\"smilies/messenger/sun.gif\" height=19 width=19><img src=\"smilies/messenger/sun.gif\" height=19 width=19><img src=\"smilies/messenger/sun.gif\" height=19 width=19><img src=\"smilies/messenger/sun.gif\" height=19 width=19><img src=\"smilies/messenger/sun.gif\" height=19 width=19>";}
if($TotalPoints > 10000){$UserRanking = "<img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19><img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19><img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19><img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19><img src=\"smilies/messenger/sm_devil.gif\" height=19 width=19>";}

 return $UserRanking;
}
?>
